package com.anbinh.doctor.fragment.clinic;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.adapter.MessagesAdapter;
import com.anbinh.doctor.fragment.FragmentMain;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.Notification;
import com.anbinh.doctor.model.NotificationList;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.IRcallBak;
import com.anbinh.doctor.utils.Utils;

public class FragmentClinicMessages extends Fragment implements
		OnScrollListener {
	public static FragmentClinicMessages fragment = null;
	private View mView;
	private UserLogin userLogin;
	protected MessagesAdapter adapter;
	protected ArrayList<Notification> mList;
	protected ListView mGridView;
	private int currentPage = 0;
	private int pageSize = 10;
	private boolean isLoadding = false;
	protected Notification currentNotification;
	private int doctorid;
	private int currentLocation;
	protected IRcallBak callIResult;
	protected boolean isLongclick;

	public static FragmentClinicMessages newInstance() {
		if (fragment == null) {
			fragment = new FragmentClinicMessages();
		}
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof IRcallBak) {
			callIResult = (IRcallBak) activity;
		}
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		userLogin = Utils.getAccountModel(getActivity(), Utils.KEY_USER);
		mView = inflater.inflate(R.layout.fragment_message, null);
		doctorid = FragmentDoctorList.newInstance().getCurrentDoctors().getId();
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mGridView = (ListView) mView.findViewById(R.id.listView1);
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				currentNotification = mList.get(position);
				mList.get(position).setStatus(1);
				adapter.notifyDataSetChanged();
				Bundle bundle = new Bundle();
				bundle.putInt(Utils.KEY, R.id.message_detailt_doctor);
				bundle.putInt(Utils.KEY_ID, currentNotification.getId());

				bundle.putBoolean(Utils.CUSTOMBAR, false);
				bundle.putString(Utils.TITLE,
						getResources().getString(R.string.chi_tiet_tin_nhan));
				Utils.getNewIntent(getActivity(), bundle);
			}

		});
		mGridView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				isLongclick = true;
				currentLocation = position;
				callIResult.onDisplay(Utils.DELETE_MESSAGE, null);
				return false;
			}

		});
		mGridView.setOnScrollListener(this);
		currentPage = 0;
		adapter = null;

		onLoadData();
	}

	public Notification getCurrentNotification() {
		return currentNotification;
	}

	public void onResetData() {
		currentPage = 0;
		adapter = null;
	}

	public void onLoadData() {
		Utils.onLoadding(mView, true);
		ServerController.getInstance(getActivity()).requestNotificationList(
				userLogin.getData().getToken(), currentPage, pageSize,
				doctorid, new OnRequest<NotificationList>() {

					@Override
					public void onSuccess(NotificationList data) {
						Utils.onLoadding(mView, false);
						if (getActivity() == null)
							return;
						if (data.getResponsecode() < 0)
							return;

						if (adapter == null) {
							mList = data.getData().getNotificationsList();
							adapter = new MessagesAdapter(getActivity(),
									R.layout.item_message, mList);
							mGridView.setAdapter(adapter);
						} else {
							mList.addAll(data.getData().getNotificationsList());
							adapter.notifyDataSetChanged();
						}

						currentPage = data.getData().getPaging()
								.getCurrentPage();
						int lastPage = data.getData().getPaging().getLastPage();
						if (currentPage < lastPage) {
							isLoadding = false;
						} else {
							isLoadding = true;
						}
					}

					@Override
					public void onError(int errorCode, String message) {
						Utils.onLoadding(mView, false);

					}
				});
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		int lastInScreen = firstVisibleItem + visibleItemCount;
		if ((lastInScreen == totalItemCount)) {
			if (!isLoadding) {
				onLoadData();
				isLoadding = true;
			}
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub

	}

	public void onDelete() {
		if (currentLocation == -1)
			return;

		Utils.onLoadding(mView, true);
		UserLogin userLogin = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);
		if (userLogin == null)
			return;
		final Notification item = mList.get(currentLocation);

		ServerController.getInstance(getActivity()).requestDeleteMessage(
				userLogin.getData().getToken(), item.getId(),
				FragmentDoctorList.newInstance().getCurrentDoctors().getId(),
				new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						Utils.onLoadding(mView, false);
						if (getActivity() == null)
							return;
						if (data.getResponsecode() >= 0) {
							mList.remove(item);
							adapter.notifyDataSetChanged();
						}

					}

					@Override
					public void onError(int errorCode, String message) {
						Utils.onLoadding(mView, false);
					}
				});
	}

	public void setLongclick(boolean b) {
		isLongclick=b;
	}

}
